﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using NotebookS2.Sales.Dialogs; 
using CNotebook;
using CNotebook.Sales;

namespace NotebookS2
{
    public partial class FND_RET : RibbonForm
    {
        public FND_RET()
        {
            InitializeComponent();
        }
        public string ConnectionString { get; set; }
        public string DocumentNumber { get; set; }
        public double TotalBalance { get; set; }
        void BindGridByDocument(string _DocNumber)
        {
            SalesReturnManager oManager = new SalesReturnManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.BrowseReturnsByDocumentNumber(_DocNumber);
            gridControl1.Refresh();

            oManager.Close();
        }

        void BindGridByCustomer(string _CUSTOMERNAME)
        {
            SalesReturnManager oManager = new SalesReturnManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.BrowseReturnsByCustomer(_CUSTOMERNAME);
            gridControl1.Refresh();

            oManager.Close();
        }

        void BindGridByCustomerCode(string _CUST_CODE)
        {
            SalesReturnManager oManager = new SalesReturnManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.BrowseReturnsByCustomerCode(_CUST_CODE);
            gridControl1.Refresh();

            oManager.Close();
        }

        void BindGridByIRS(string _IRS_NUMBER)
        {
            SalesReturnManager oManager = new SalesReturnManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.BrowseReturnsByIRS(_IRS_NUMBER);
            gridControl1.Refresh();

            oManager.Close();
        }

        void BindGridByRTV(string _RTV_NUMBER)
        {
            SalesReturnManager oManager = new SalesReturnManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.BrowseReturnsByRTV(_RTV_NUMBER);
            gridControl1.Refresh();

            oManager.Close();
        }
     
        private void FND_RET_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(typeof(WaitForm5));

            if (cboSearch.SelectedIndex == 0)
            {
                BindGridByDocument(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 1)
            {
                BindGridByCustomerCode(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 2)
            {
                BindGridByCustomer(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 3)
            {
                BindGridByIRS(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 4)
            {
                BindGridByRTV(txtSearch.Text);
            }

            DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            int iRowIndex = gridView1.FocusedRowHandle;

            this.DocumentNumber = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["DocNum"]).ToString();
            this.TotalBalance = Convert.ToDouble(gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["TotalBalance"]));

            this.Close();
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (cboSearch.SelectedIndex == 0)
                {
                    BindGridByDocument(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 1)
                {
                    BindGridByCustomerCode(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 2)
                {
                    BindGridByCustomer(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 3)
                {
                    BindGridByIRS(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 4)
                {
                    BindGridByRTV(txtSearch.Text);
                }
            }

            if (e.KeyCode == Keys.Tab)
            {
                if (cboSearch.SelectedIndex == 0)
                {
                    BindGridByDocument(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 1)
                {
                    BindGridByCustomerCode(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 2)
                {
                    BindGridByCustomer(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 3)
                {
                    BindGridByIRS(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 4)
                {
                    BindGridByRTV(txtSearch.Text);
                }
            }
        }
    }
}
